package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.module.common.servermanager.ServerManager;
import com.motu.monstercity.protocol.Common.PBRankInfo;
import com.motu.monstercity.protocol.Common.PBRankMember;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameRank.CSGameRankListRequest;
import com.motu.monstercity.protocol.CsGameRank.CSGameRankListResponse;
import com.motu.monstercity.protocol.CsGameRank.CSGameRankWorshipRequest;
import com.motu.monstercity.protocol.CsGameRank.CSGameRankWorshipResponse;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.commondata.rank.RankConstant;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;

import java.util.List;

public class RankHandler {
	// CMD_GAME_RANK_LIST = 71801;// 查看榜单 公共数据. [2]每次打开请求
	public static Object CMD_GAME_RANK_LIST(RequestParam clientParam) throws Exception {
		CSGameRankListRequest request = CSGameRankListRequest.parseFrom(clientParam.getData());
		CSGameRankListResponse.Builder response = CSGameRankListResponse.newBuilder();
		long userId = clientParam.getUserId();
		int rankType = request.getRankType();// 榜单类型
		String dateKey = request.getDateKey(); // 榜单key (如果是活动类型的榜单，则传活动ID；如果系统榜单的话，则传日期，不传则需单独处理)

		UserInfo userInfo = GameUser.getUserInfo(userId);
		if (Tool.isEmpty(dateKey)) {
			dateKey = RankManager.getDateKey(rankType);
		}
		int serverId = userInfo.getServerId();
		if (RankConstant.isCrossRank(rankType)) {// 跨服排行榜，则取服务器的分组id
			serverId = ServerManager.getWarZoneId(serverId);
		}
		List<PBRankMember.Builder> memberList = RankManager.getRankMemberList(rankType, serverId, dateKey, AllParam.RANK_MEMBER_LIST_MAX);
		PBRankInfo.Builder pbRankInfo = PBRankInfo.newBuilder();
		for (PBRankMember.Builder pbRankMember : memberList) {
			boolean isSend = true;// 是否下发
			if (rankType == RankConstant.SERVER_RANK_CAVE) {
				int floorId = (int) pbRankMember.getScore();// 层数
				if (floorId == 1) {// 滞留层的不下发
					isSend = false;
				}
			} else if (rankType == RankConstant.SERVER_RANK_MAX_CHILD) {// 子嗣热度榜，多下发子嗣名称
				UserChildInfo userChildInfo = ChildManager.getUserChildInfo(pbRankMember.getUserId());
				if (userChildInfo != null) {
					pbRankMember.setParam(userChildInfo.takeMaxEarnChildName());
				}
			}
			if (isSend) {
				pbRankInfo.addMembers(pbRankMember.build());
			}
		}
		UserUnion userUnion = UnionManager.getUserUnion(userId);
		int myRank = RankManager.getMyRank(rankType, serverId, userId, dateKey);
		if (rankType == RankConstant.ACTIVITY_CROSS_RANK_UNION_SCUFFLE) {
			myRank = RankManager.getMyRank(rankType, serverId, userUnion.getUnionId(), dateKey);
		}
		pbRankInfo.setMyRank(myRank);
		long myScore = RankManager.getMyScore(rankType, serverId, userId, dateKey);
		if (rankType == RankConstant.ACTIVITY_CROSS_RANK_UNION_SCUFFLE) {
			myScore = RankManager.getMyScore(rankType, serverId, userUnion.getUnionId(), dateKey);
		}
		pbRankInfo.setMyScore(myScore);

		response.putRankInfo(rankType, pbRankInfo.build());
		return response;
	}

	// CMD_GAME_RANK_WORSHIP = 71802;// 膜拜榜单获得奖励
	public static Object CMD_GAME_RANK_WORSHIP(RequestParam clientParam) throws Exception {
		CSGameRankWorshipRequest request = CSGameRankWorshipRequest.parseFrom(clientParam.getData());
		CSGameRankWorshipResponse.Builder response = CSGameRankWorshipResponse.newBuilder();
		long userId = clientParam.getUserId();

		int rankType = request.getRankType();// 榜单类型

		UserAffair userAffair = GameUser.getUserAffair(userId);
		JsonArray jsonArray = userAffair.getWorshipArray();
		if (Tool.isInList(jsonArray, rankType)) {
			return ErrorWord.REPEATED_OPERATION;// 已经膜拜过了
		}

		// 执行逻辑
		PBUserData.Builder pbUserData = response.getUserBuilder();
		UserInfo userInfo = GameUser.getUserInfo(userId);
		int itemId = AllParam.RANK_WORSHIP_REWARD[0];
		int num = AllParam.RANK_WORSHIP_REWARD[1];
		RewardManager.addReward(userInfo, itemId, num, pbUserData, LogType.RANK_WORSHIP);
		jsonArray.add(rankType);
		userAffair.putWorship(jsonArray.toString());
		userAffair.update();
		ProtoDataUtils.updatePBUserData(pbUserData, userAffair);

		MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_8, 1);// 更新主线任务,成就,日常任务的进度
		MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_158, 1);// 更新主线任务,成就,日常任务的进度
		MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_169, 1);// 更新主线任务,成就,日常任务的进度
		RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_8, 1);
		RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_158, 1);
		RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_169, 1);

		return response;
	}
}
